let express = require("express");
let router = express.Router();
let constants = require("../utils/constants");
const { ohana } = require("ohana-node-orm");
const tableName = "SAP_JIANGDU_COMPANY_PORTRAITS";
const ohana_instance = new ohana(tableName);
const ohana_instance_user = new ohana(constants.REGISTEDUSERSTABLE);
const { checkAuth } = require("../utils/utils");
let log = require("cf-nodejs-logging-support");
log.setLoggingLevel("info");


router.put("/setTop", function (req, res, next) {
    const user = checkAuth(req.user, constants.BASICAUTH);

    if (!user.authCheck) {
        res.sendStatus(403);
    }
    const { data } = req.body;
    setTop(data).then(result => {
        if (result > 0) {
            res.sendStatus(200);
        } else {
            res.sendStatus(400);
        }
    })


});
async function setTop(data) {
    var result = "";
    console.log(data)
    if (data.COMPANY_ID) {
        result = await ohana_instance_user.raw("update GLOBAL_PARAMETER set SHOW_COMPANY_ID = '" + data.COMPANY_ID + "'");
    } else {
        result = await ohana_instance_user.raw("update GLOBAL_PARAMETER set SWITCH_ON_BLACKLIST = " + data.SWITCH_ON_BLACKLIST);
    }
    return result;
}

router.get("/", function (req, res, next) {
    // #swagger.tags = ['描述']
    // #swagger.summary = '获取列表'
    // #swagger.description = '需要JWT Token'
    /* #swagger.security = [{
                   "JiangduJWT": []
      }] */
    const user = checkAuth(req.user, constants.BASICAUTH);

    // if (!user.authCheck) {
    //     res.sendStatus(403);
    // }

    let payload = {};
    const { COMPANY_ID } = req.query;
    if (!COMPANY_ID) {
        payload.COMPANY_CODE_COMPANY_CODE = user.user_id;
    } else {
        payload.COMPANY_CODE_COMPANY_CODE = COMPANY_ID;
    }

    getData(payload)
        .then((result) => {
            res.send(result);
        })
        .catch((err) => {
            log.error(err);
            res.status(500).send(err);
        });
});

async function getData(condition) {
    let result = await ohana_instance.find(condition);
    return result;
}

router.get("/top", function (req, res, next) {
    // #swagger.tags = ['描述']
    // #swagger.summary = '获取该行业前三企业'
    // #swagger.description = '需要JWT Token'
    /* #swagger.security = [{
                   "JiangduJWT": []
      }] */
    const user = checkAuth(req.user, constants.BASICAUTH);

    // if (!user.authCheck) {
    //     res.sendStatus(403);
    // }

    let { CAP, INDUSTRY } = req.query;
    getTop(CAP, INDUSTRY)
        .then((result) => {
            res.send(result);
        })
        .catch((err) => {
            log.error(err);
            res.status(500).send(err);
        });
});

async function getTop(CAP, INDUSTRY) {
    let result = [];
    if (CAP) {
        if (INDUSTRY) {
            result = await ohana_instance_user.raw(
                "SELECT A1.*, A2.OVERALL_RANKING, A2.BASIC_ABILITY, A2.JINGYING_MANAGEMENT, A2.TECH_INNO, A2.IOT,A2.CONTINOUS_DEVELOP, A2.CREDIT_RISK FROM SAP_JIANGDU_USERS as A1 LEFT JOIN SAP_JIANGDU_COMPANY_PORTRAITS as A2 ON A1.COMPANY_NAME = A2.COMPANY_CODE_COMPANY_CODE WHERE A1.CAP  ='" +
                CAP +
                "' AND A1.INDUSTRY  ='" +
                INDUSTRY +
                "'ORDER BY A2.OVERALL_RANKING DESC LIMIT 3"
            );
        } else {
            result = await ohana_instance_user.raw(
                "SELECT A1.*, A2.OVERALL_RANKING, A2.BASIC_ABILITY, A2.JINGYING_MANAGEMENT, A2.TECH_INNO, A2.IOT,A2.CONTINOUS_DEVELOP, A2.CREDIT_RISK FROM SAP_JIANGDU_USERS as A1 LEFT JOIN SAP_JIANGDU_COMPANY_PORTRAITS as A2 ON A1.COMPANY_NAME = A2.COMPANY_CODE_COMPANY_CODE WHERE A1.CAP  ='" +
                CAP +
                "' ORDER BY A2.OVERALL_RANKING DESC LIMIT 3"
            );
        }
    } else {
        result = await ohana_instance_user.raw(
            "SELECT A1.*, A2.OVERALL_RANKING, A2.BASIC_ABILITY, A2.JINGYING_MANAGEMENT, A2.TECH_INNO, A2.IOT,A2.CONTINOUS_DEVELOP, A2.CREDIT_RISK FROM SAP_JIANGDU_USERS as A1 LEFT JOIN SAP_JIANGDU_COMPANY_PORTRAITS as A2 ON A1.COMPANY_NAME = A2.COMPANY_CODE_COMPANY_CODE WHERE A1.INDUSTRY  ='" +
            INDUSTRY +
            "' ORDER BY A2.OVERALL_RANKING DESC LIMIT 3"
        );
    }

    return result;
}

module.exports = router;
